package com.bz.controller;


import com.bz.entity.Emp;
import com.bz.service.impl.EmpServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("emp")
@CrossOrigin
@Slf4j
public class EmpController {

    @Autowired
    private EmpServiceImpl empService;

    @Value("${photo.dir}")
    private String realPath;


    @GetMapping("findAll")
    public List<Emp> findAll() {
        return empService.findAll();
    }


    @RequestMapping("/save")
    public Map<String, Object> save(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息[{}]", emp.toString());
        log.info("头像信息[{}]", photo.getOriginalFilename());

        Map<String, Object> map = new HashMap<>();

        try {
            String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath, newFileName));
            emp.setPath(newFileName);
            empService.save(emp);
            map.put("state", true);
            map.put("msg", "员工信息保存成功");


        } catch (Exception e) {
            map.put("state", false);
            map.put("msg", "员工信息保存失败");


        }


        return map;
    }

    @RequestMapping("delete")
    public Map<String, Object> delete(String id) {
        log.info("删除的员工id[{}]", id);
        Map<String, Object> map = new HashMap<>();

        try {

            Emp emp = empService.findOne(id);

            File file = new File(realPath, emp.getPath());

            if (file.exists()) {
                file.delete();
            }


            empService.delete(id);
            map.put("stata", true);
            map.put("msg", "删除成功");
        } catch (Exception e) {
            map.put("stata", false);
            map.put("msg", "删除失败");
        }
        return map;
    }

    @RequestMapping("findOne")
    public Emp findOne(String id) {
        Emp one = empService.findOne(id);
        return one;
    }

    @RequestMapping("update")
    public Map<String, Object> update(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息[{}]", emp.toString());


        Map<String, Object> map = new HashMap<>();

        try {

            if (photo!=null&&photo.getSize()!=0){
                log.info("头像信息[{}]", photo.getOriginalFilename());
                String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath, newFileName));
                emp.setPath(newFileName);

            }


            empService.update(emp);
            map.put("state", true);
            map.put("msg", "员工信息更新成功");


        } catch (Exception e) {
            map.put("state", false);
            map.put("msg", "员工信息保存失败");


        }


        return map;
    }


    @GetMapping("findByPage")
    public Map<String, Object> findByPage(Integer pageNow, Integer pageSize) {
        log.info("findByPage:"+pageNow);
        Map<String, Object> map = new HashMap<>();
        pageNow = pageNow == null ? 1 : pageNow;
        pageSize = pageSize == null ? 2 : pageSize;
        List<Emp> list = empService.findByPage(pageNow, pageSize);

        Long totals = empService.findTotals();
        map.put("users", list);
//        map.put("pageCount", totals%pageSize==0?totals%pageSize:totals%pageSize+1);
        map.put("totals", totals);

        return map;

    }

}
